<template>
  <div class="icon-body">
    <el-input
      v-model="name"
      placeholder="请输入图标名称"
      clearable
      @input.native="filterIcons"
      @clear="filterIcons"
    />
    <div class="icon-list">
      <el-row>
        <el-col v-for="(item, index) in iconList" :key="index" :span="8">
          <span class="icon-content" @click="selectIcon(item)">
            <svg-icon :icon-class="item" style="margin-right:5px" />
            <span>{{ item }}</span>
          </span>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import iconList from './iconList'

export default {
  data() {
    return {
      name: '',
      iconList
    }
  },
  methods: {
    filterIcons() {
      if (this.name) {
        this.iconList = this.iconList.filter(item => item.includes(this.name))
      } else {
        this.iconList = iconList
      }
    },
    selectIcon(name) {
      this.$emit('selectIcon', name)
      // 关闭图标选择框
      document.body.click()
    },
    reset() {
      this.name = ''
      this.iconList = iconList
    }
  }
}
</script>

<style lang="scss" scoped>
  .icon-body {
    width: 100%;
    padding: 10px;
  }
  .icon-list {
    height: 200px;
    overflow-y: scroll;
    padding: 5px 0;
  }
  .icon-content {
    cursor: pointer;
    display: inline-block;
    height: 30px;
    line-height: 30px;
  }
</style>
